﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.API.Framework;
using Sealight.Data;
using Sealight.API.Ioc;
using Sealight.Core.Domain;

namespace Sealight.CoreAPI
{
  public class ShoppingCartService:IShoppingCartService
    {        
        public Core.DataAdapter.shopping_cart_item AddCartItem(Core.DataAdapter.shopping_cart_item item)
        {
            if (item == null)
                throw new ArgumentNullException("item data is Null.");
            //persists
            #region start_unit_of_work
            IUnitOfWork uow = null;
            if (!UnitOfWork.IsStarted)
                uow = UnitOfWork.Start();
            else uow = UnitOfWork.Current;
            #endregion
            bool commitTrans = false;
            if (!uow.IsInActiveTransaction)
            {
                //if not in active transaction
                //then begin transaction and commit it later
                uow.BeginTransaction(); //start transaction
                commitTrans = true;
            }
            try
            {
                IProductDataRepository repoP = IocContainer.Container().Resolve<IProductDataRepository>();
                IShoppingCartDataRepository repo = IocContainer.Container().Resolve<IShoppingCartDataRepository>();
                IRepository<UserSessionEntity> repoUS = IocContainer.Container().Resolve<IRepository<UserSessionEntity>>();
                ShoppingCartItemEntity data= repo.GetById(item.id);

                if (data == null)
                    data = new ShoppingCartItemEntity();
                data.ShoppingCartType = item.shopping_cart_type;
                data.Attributes = item.attributes;
                data.CreatedDate = item.created_date;
                data.ModifiedDate = item.modified_date;
                data.Quantity = item.quantity;

                if (item.product_variant != null)
                    data.ProductVariant = repoP.GetProductVariantByID(item.product_variant.id);
                if (item.user_session != null)
                    data.UserSession = repoUS.GetById(item.user_session.id);

                repo.SaveOrUpdate(data);
                if (commitTrans)
                    uow.TransactionalFlush();

                item = data.ToDataAdapter();
            }
            catch (Exception ex) { throw new Exception("Error AddCartItem data. ErrMsg: " + ex.Message, ex); }
            return item;
        }

        public IList<Core.DataAdapter.shopping_cart_item> GetCartItemsByUser(Guid user_id)
        {
            if (!UnitOfWork.IsStarted)
                UnitOfWork.Start();

            IShoppingCartDataRepository repo = IocContainer.Container().Resolve<IShoppingCartDataRepository>();
            IList<Core.DataAdapter.shopping_cart_item> result= null;

            IList<ShoppingCartItemEntity> data = repo.GetCartItemsByUser(user_id);

            if (data != null)
                result = (from c in data select c.ToDataAdapter()).ToList();
            return result;
        }

        public IList<Core.DataAdapter.shopping_cart_item> GetCartItemsByUserSession(Guid user_session_id)
        {
            if (!UnitOfWork.IsStarted)
                UnitOfWork.Start();

            IShoppingCartDataRepository repo = IocContainer.Container().Resolve<IShoppingCartDataRepository>();

            IList<Core.DataAdapter.shopping_cart_item> result = null;
            IList<ShoppingCartItemEntity> data = repo.GetCartItemsByUserSession(user_session_id);

            if (data != null)
                result = (from c in data select c.ToDataAdapter()).ToList();
            return result;
        }
    }
}
